<template>
  <div>
    <div class="list-table mb-15">
      <div class="area-header">
        <span class="area-header-title">{{ $t('出资主体所占估值变动') }}</span>
      </div>
      <Empty :data.sync="chartData.datas">
        <lineChart v-if="chartData.datas.length > 0" height="300px" :chart-data="chartData" />
      </Empty>
    </div>
    <div class="list-table">
      <div class="area-header">
        <span class="area-header-title">{{ $t('基金估值') }}</span>
      </div>
      <el-form ref="queryForm" :model="queryParams" :inline="true">
        <el-form-item :label="$t('估值时间')" prop="valuationTime">
          <DatePicker v-model="valuationTimes" type="daterange" />
        </el-form-item>
        <!--      <el-form-item :label="$t('估值结果') + $t('金额单位')" prop="valuationAmount">-->
        <!--        <Money v-model="queryParams.valuationAmount" unit="w" :placeholder="$t('估值金额')" />-->
        <!--      </el-form-item>-->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['bf:valuation:edit']"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="addHandle"
          >{{ $t('新增') }}
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            :v-bind:v-has-permi="['bf:valuation:import']"
            type="info"
            icon="el-icon-upload2"
            size="mini"
            @click="handleImport"
          >{{ $t('导入') }}</el-button>
        </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          v-hasPermi="['bf:valuation:export']"-->
      <!--          type="warning"-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="exportHandle"-->
      <!--        >{{ $t('导出') }}</el-button>-->
      <!--      </el-col>-->
      </el-row>

      <el-table v-loading="loading" tooltip-effect="darkTable" :data="dealBfValuationList" border>
        <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
          <template slot-scope="scope">
            {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('估值时间')" align="center" prop="valuationTime" width="180">
          <template slot-scope="scope">
            <span class="link-type" @click="updateHandle(scope.row)">{{ parseTime(scope.row.valuationTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('出资主体所占估值') + $t('金额单位')" align="center" prop="valuationAmount" width="180">
          <template slot-scope="scope">
            <span class="money-text">{{ moneyFormat(scope.row.valuationAmount, 'w') }}</span>
          </template>
        </el-table-column>
        <!--<el-table-column :label="$t('应对方案')" align="center" prop="responsePlan" />-->
        <el-table-column :label="$t('备注')" align="center" prop="note" />
        <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
          <template slot-scope="scope">
            <el-button
              v-hasPermi="['bf:valuation:edit']"
              size="small"
              type="text"
              icon="el-icon-edit"
              @click="updateHandle(scope.row)"
            >{{ $t('修改') }}
            </el-button>
            <el-button
              v-hasPermi="['bf:valuation:remove']"
              size="small"
              type="text"
              icon="el-icon-delete"
              @click="deleteHandle(scope.row)"
            >{{ $t('删除') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" :close-on-click-modal="false" :lock-scroll="false" append-to-body>
      <div v-loading="upload.isUploading">
        <el-upload
          ref="upload"
          :limit="1"
          accept=".xlsx, .xls"
          :headers="upload.headers"
          :action="upload.url + '?updateSupport=' + upload.updateSupport+ '&dealId=' + upload.dealId"
          :disabled="upload.isUploading"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
          :auto-upload="false"
          drag
        >
          <i class="el-icon-upload" />
          <div class="el-upload__text">
            {{ $t('将文件拖到此处，或') }}
            <em>{{ $t('点击上传') }}</em>
          </div>
          <div slot="tip" class="el-upload__tip" style="color:red">{{ $t('提示：仅允许导入“xls”或“xlsx”格式文件！') }}</div>
        </el-upload>
        <div slot="footer" class="dialog-footer" style="position: relative;text-align: right;">
          <el-button type="info" class="el-icon-download el-icon--right" style="font-size:12px;float:left;" @click="fetchTemplate">
            {{ $t('下载模板') }}
          </el-button>
          <el-button @click="upload.open = false">{{ $t("取消") }}</el-button>
          <el-button type="primary" @click="$refs.upload.submit(); upload.loading = true">{{ $t("确定") }}</el-button>
        </div>
      </div>
    </el-dialog>
    <!-- 添加或修改基金估值对话框 -->
    <el-dialog
      :title="$t('基金估值')"
      :visible.sync="formOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      custom-class="dialogForm"
      append-to-body
    >
      <dealBfValuationForm ref="dealBfValuationForm" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listDealBfValuation,
  delDealBfValuation,
  exportDealBfValuation
} from '@/api/deal/bf/valuation/dealBfValuation.js'
import dealBfValuationForm from './dealBfValuationForm.vue'
import lineChart from '@/views/chart/lineChart.vue'
import { getToken } from '@/utils/auth'

export default {
  components: {
    dealBfValuationForm,
    lineChart
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 基金估值表格数据
      dealBfValuationList: [],
      // 是否显示弹出层
      formOpen: false,
      // 查询参数
      queryParams: {
        dealId: this.$route.query.id,
        valuationTime: undefined,
        valuationAmount: undefined,
        responsePlan: undefined,
        note: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 查询参数
      valuationTimes: [],
      optionData: {
        xData: [],
        yData: []
      },
      // 导入参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        dealId: this.$route.query.id,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/valuation/dealBfValuation/importData'
      },
      // 表单校验
      rules: {
        valuationTime: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], valuationAmount: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], responsePlan: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ]
      },
      chartData: {
        datas: []
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询基金估值列表 */
    getList() {
      this.loading = true
      if (this.valuationTimes != null && this.valuationTimes.length > 1) {
        this.queryParams.startDate = this.valuationTimes[0]
        this.queryParams.endDate = this.valuationTimes[1]
      } else {
        this.queryParams.startDate = undefined
        this.queryParams.endDate = undefined
      }
      listDealBfValuation(this.queryParams).then(response => {
        this.dealBfValuationList = response.rows

        var xData = []
        var yData = []
        for (var i = this.dealBfValuationList.length - 1; i >= 0; i--) {
          const data = this.dealBfValuationList[i]
          xData.push(this.parseTime(data.valuationTime))
          yData.push(data.valuationAmount)
        }
        this.chartData.xLabel = xData
        this.chartData.valueFormatter = true
        this.chartData.datas = [
          {
            name: window.i18n.t('估值结果'),
            data: yData
          }
        ]

        this.total = response.total
        // 添加表格数据
        this.loading = false
      })
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealId: this.$route.query.id,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        valuationTime: undefined,
        valuationAmount: undefined,
        responsePlan: undefined,
        note: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.dealId = this.$route.query.id
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.valuationTimes = []
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.dealBfValuationForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.dealBfValuationForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return delDealBfValuation(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {
      })
    },
    /** 导入 **/
    // 导入
    handleImport() {
      this.upload.title = this.$t('导入')
      this.upload.open = true
    },
    // 文件上传中处理
    handleFileUploadProgress() {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert(response.msg, this.$t('导入结果'), { dangerouslyUseHTMLString: true })
      this.getList()
    },
    // 下载模板
    fetchTemplate() {
      var href1 = window.location.href
      var href2 = this.$route.path
      var url = href1.substring(0, href1.indexOf(href2))
      window.location.href = url + '/template/valuationOfParticipatingFundFunds.xlsx'
    },
    /** 导出按钮操作 */
    exportHandle() {
      const queryParams = this.queryParams
      this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return exportDealBfValuation(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    }

  }
}
</script>
